package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;

import cn.stylefeng.guns.sys.modular.system.entity.BtyMdCaiwu;
import cn.stylefeng.guns.sys.modular.system.entity.BtyMdSfc;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyMdCaiwuMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyMdSfcMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyMdSfcParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyMdSfcResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtySfcDto;
import cn.stylefeng.guns.sys.modular.system.service.BtyMdSfcService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * 收发存 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-10-25
 */
@Service
public class BtyMdSfcServiceImpl extends ServiceImpl<BtyMdSfcMapper, BtyMdSfc> implements BtyMdSfcService {

    @Resource
    private BtyMdCaiwuMapper btyMdCaiwuMapper;
    @Override
    public void add(BtyMdSfcParam param){
        BtyMdSfc entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyMdSfcParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyMdSfcParam param){
        BtyMdSfc oldEntity = getOldEntity(param);
        BtyMdSfc newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyMdSfcResult findBySpec(BtyMdSfcParam param){
        return null;
    }

    @Override
    public List<BtyMdSfcResult> findListBySpec(BtyMdSfcParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyMdSfcParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 导入收发存
     * @param list
     */
    @Override
    public void insertSfc(List<BtySfcDto> list) {
        List<BtyMdCaiwu> shopList = new ArrayList<>();
        List<Long> cwIdList = new ArrayList<>();

        if (ToolUtil.isNotEmpty(list)){
            for (BtySfcDto sfcDto :list) {
                BtyMdCaiwu btyMdCaiwu = new BtyMdCaiwu();
                btyMdCaiwu.setBtyMdCaiwuId(sfcDto.getBtyMdCaiwuId());//财务id
                btyMdCaiwu.setYaoma(sfcDto.getYaoma());//妖码
                btyMdCaiwu.setDm(sfcDto.getShopName());//门店名
                btyMdCaiwu.setKjqj(sfcDto.getKjqj());//会计月
                btyMdCaiwu.setFlagSfc(1);//存在收发存数据
                if(!shopList.contains(btyMdCaiwu)){
                    shopList.add(btyMdCaiwu);
                    cwIdList.add(sfcDto.getBtyMdCaiwuId());
                }
            }

            //财务表中写入数据
            List<BtyMdCaiwu> list1 = btyMdCaiwuMapper.getCw(cwIdList);//财务表中是否存在
            if (ToolUtil.isNotEmpty(list1)){//存在 则修改状态
                //批量修改财务表中存在的数据
                btyMdCaiwuMapper.updateCw(list1);

                List<BtyMdCaiwu> list2 = new ArrayList<>();
                //获取财务表中没有的数据
                shopList.forEach(btyMdCaiwu -> {
                    if (list1.stream().filter(w->w.getBtyMdCaiwuId().equals(btyMdCaiwu.getBtyMdCaiwuId())).findAny().isPresent()) {
                        list2.add(btyMdCaiwu);
                    }
                });
                // 差集 (shopList - list2)
                List<BtyMdCaiwu> list3 = shopList.stream().filter(item -> !list2.contains(item)).collect(toList());

                if (ToolUtil.isNotEmpty(list3)){
                    btyMdCaiwuMapper.insertCw(list3);
                }
            }else {//不存在 则新增数据

                if (ToolUtil.isNotEmpty(shopList)){
                    for (int i = 0; i < shopList.size();){
                        int i2 = (i + 200);
                        i2 = i2 > shopList.size() ? shopList.size() : i2;
                        List<BtyMdCaiwu> sub = shopList.subList(i, i2);
                        btyMdCaiwuMapper.insertCw(sub);
                        i = i2;
                    }
//                    btyMdCaiwuMapper.insertCw(shopList);
                }
            }

            //TODO-6 收发存表写入数据
            if (CollectionUtil.isNotEmpty(list)){
                //每90条保存一次
                for(int i = 0; i < list.size();){
                    int i2 = (i + 90);
                    i2 = i2 > list.size() ? list.size() : i2;
                    List<BtySfcDto> sub = list.subList(i, i2);
                    baseMapper.insertSfc(sub);
                    i = i2;
                }

//                //收发存表写入数据
//                baseMapper.insertSfc(list);
            }
        }
    }

    private Serializable getKey(BtyMdSfcParam param){
        return param.getBtyMdSfcId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyMdSfc getOldEntity(BtyMdSfcParam param) {
        return this.getById(getKey(param));
    }

    private BtyMdSfc getEntity(BtyMdSfcParam param) {
        BtyMdSfc entity = new BtyMdSfc();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
